package jinyilw.swing.visuals.comp;

import jinyilw.swing.ComponentTools;
import jinyilw.swing.graphics.GraphicsTools;
import jinyilw.swing.graphics.RenderingConfig;
import jinyilw.swing.rlaf.theme.RThemeColor;

import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.Composite;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;

public class ColorComp extends MouseActComp implements SwingConstants
{
	private static final long serialVersionUID = -7970338056562367418L;
	// private static Color pColor1 = new Color(100, 100, 100);
	private static final Color blackColor = new Color(0, 0, 0, 50);
	private static final Color lightColor = new Color(255, 255, 255, 100);
	private Color[] gradientColors;
	private int direction = VERTICAL;

	private GradientPaint borderPaint;
	private RoundRectangle2D.Float roundRect;
	private int arc;

	private final RenderingConfig renderingConfig = new RenderingConfig();

	public ColorComp()
	{
	}

	public ColorComp(Color c)
	{
		setBackground(c);
	}

	public ColorComp(Color c, boolean b)
	{
		setBackground(c);
		setRolloverEnable(b);
	}

	public ColorComp(Color[] colors)
	{
		gradientColors = colors;
	}

	public void setGradientColors(Color[] colors)
	{
		gradientColors = colors;
	}

	public void setDirection(int direction)
	{
		this.direction = direction;
	}

	public Color[] getGradientColors()
	{
		return gradientColors;
	}

	public void copyColors(Color[] colors)
	{
		if (gradientColors == null || colors == null)
			return;
		System.arraycopy(gradientColors, 0, colors, 0, colors.length);
	}

	@Override
	public void setBounds(int x, int y, int width, int height)
	{
		boolean unResized = width == getWidth() && height == getHeight();
		super.setBounds(x, y, width, height);
		if (unResized)
			return;
		arc = (int) (height * 0.8);
		roundRect = new RoundRectangle2D.Float(1, 1, width - 3, height - 3, arc,
				arc);
		borderPaint = new GradientPaint(1, 1, lightColor, 1, height - 2,
				blackColor);
	}

	@Override
	public void paint(Graphics g)
	{
		int h = getHeight() - 3, w = getWidth() - 3;
		Graphics2D g2d = (Graphics2D) g;
		Composite composite = ComponentTools.applyEnableAlpha(g2d, isEnabled(),
				1f, 0.1f);
		renderingConfig.setAAPainting(g2d);

		if (gradientColors != null && gradientColors.length > 1)
		{
			Shape saveClip = g2d.getClip();
			g2d.clip(roundRect);
			GraphicsTools.fillGradientColor(g2d, gradientColors, 1, 1, w, h,
					direction);
			g2d.setClip(saveClip);
		} else
		{
			g2d.setColor(getBackground());
			g2d.fill(roundRect);
		}

		if (isRollover)
		{
			g2d.setColor(RThemeColor.themeColor);
			g2d.draw(roundRect);
		} else
		{
			g2d.setPaint(borderPaint);
			g2d.draw(roundRect);
		}

		renderingConfig.resetAAPainting(g2d);
		if (composite != null)
			g2d.setComposite(composite);
	}
}
